/**
 * ResourceTranslationServiceImpl.java
 *
 * Copyright (C) 2009,  Tomislav Milkovic
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package extjsdyntran.translation.services;


import java.util.HashMap;
import java.util.Map;

import extjsdyntran.translation.ITranslationService;
import extjsdyntran.translation.ServiceTypes;
import extjsdyntran.translation.TranslationFactory;
import extjsdyntran.translation.TranslationServiceAbstract;
import extjsdyntran.translation.store.StoreManager;

/**
 * @description Resource Translation Service 
 * Resource names are mapped to default strings. 
 * Default strings are translated by other translation services to the requested language translation.
 * This allows to set resource id's to components instead of real text values
 * 
 * @version 1.7
 * 
 * this could also extend custom service to enable grouping of resource strings
 */
public final class ResourceTranslationServiceImpl extends TranslationServiceAbstract {

	private static final long serialVersionUID = 1L;
	private static final String RESOURCE_KEY = "RESOURCES";
	private Map<String,String> customlang = null; 

	public ResourceTranslationServiceImpl(String username, String password,	String _path) {
		super(username, password, _path);
		customlang = new HashMap<String,String>();
		customlang.put(RESOURCE_KEY, "res");
		//this.translations.put(RESOURCE_KEY, new Properties());
		loadLanguages();
	}
	
	public boolean isNewLanguageSupported(){
		return true;
	}


	//should check for all services as it is only resource proxy to translation service
	//and holds only resource string id's with default values
	// first part is for Translate editor; checks for RESOURCE GROUP if supported 
	public ServiceTypes isLanguageSupported(String language){
		ServiceTypes status =  customlang.containsKey(language) ? ServiceTypes.PROXY  : null;
		if(status!=null) return status;
		
		ITranslationService service = TranslationFactory.getServiceByType(language,ServiceTypes.NORMAL);
		
		return service!=null ? ServiceTypes.NORMAL : null;
	}
	

	public void loadLanguages() {
		super.loadLanguages(super.enumerationToStringArray(customlang));
	}
	

	public boolean test() {
		return true;
	}

	//for now do nothing, for future version when multiple resource groups available 
	public boolean removeLanguageFromCache(String language){
			
			if (!translations.containsKey(language)) {
				logger.warn("Error removing NULL resource!");
				return false;
			}	
			
			logger.info("Removing resource {} from storage!",language);
			if (StoreManager.clearTranslation(this,language)){
				translations.remove(language);
				customlang.remove(language);
				logger.info("Resource {} removed from storage!",language);
				return true;	
			}
			logger.warn("Resource {} NOT removed from storage!",language);
			return false;				
	}

	//for now do nothing, for future version when multiple resource groups available
	public boolean appendLanguageToCache(String language){
		if ( customlang.containsKey(language)) {
			return false;
		}		
		
		customlang.put(language,language.substring(0, 3));
		Map<String,String>  lang = new HashMap<String,String> ();
		translations.put(language, lang);
		return true;
	}

	
	
	//parameter should receive language , not RESOURCE
	//Use default value from incoming resource string id, then try to translate that value to appropriate requested language
	//could return resourceID, resource default value or translated value
	public String translate(String resID, String language)  {		
		String resStr = null;
		try {
				
			//is it resource string
			Map<String, String>  resList =  translations.get(RESOURCE_KEY);
			if(resList!=null){ //this "if" is for future to support multiple resource groups; not needed for now; just for reminder
				resStr = (String) resList.get(resID);
				
				//yes and not exists, store it and return untranslated
				if((resID.startsWith("IDX_")) &&(resStr==null)){
					resList.put(resID, "");
					StoreManager.saveTranslation( RESOURCE_KEY, resID, "");	
					return resID;
				}
			}
			
			ITranslationService service = TranslationFactory.getServiceByType(language,ServiceTypes.NORMAL);

			if (service==null) return resID;
			
			//no, then try to translate by standard translation service
			if (resStr==null){
				return service.translate(resID, language);  
			}
			
			//yes, then translate resource string value to requested language					
			return service.translate(resStr, language);

		} catch (Exception e) {
			String msg = String.format("Error trying to translate resource string {} to language {} for value : {}", new Object[]{resID, language,resStr});
			logger.error(msg);
			logger.error("",e);
			return msg;
		}
	}


	public Map<String,String> getListOfLanguages() {
		return customlang;
	}

	public ServiceTypes getServiceType(){
		return ServiceTypes.PROXY;
	}

	
}